package c.x.jy.test;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 雪花算法
 * @author admin
 *
 */
public class IdGeneratorSnowFlake {
	private static long workerId=0;
	private long datacenterId=1;
	private   Snowflake snowflake=IdUtil.createSnowflake(workerId, datacenterId);
	
	static{
		try {
			
			workerId=NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
		} catch (Exception e) {

			workerId=NetUtil.getLocalhostStr().hashCode(); 
		}  
		
	}
	
	public synchronized long snowflakeId(){
		return snowflake.nextId();
	}
	public synchronized long snowflakeId(long workerId, long dataCenterId){
		return IdUtil.createSnowflake(workerId, dataCenterId).nextId();
	}

	
	
}
